// 新版--类型占比/排名卡
var numberFormate = iot.getNumberFormat("0.00");
var energyDevId = {
    "collectChart": {
        id:10338,
        unitTotal:"T",
        unitSingle:"kg/辆",
        titleTotal:"总碳排能源类型占比排名",
        titleSingle:"单车碳排能源类型占比排名"
    },//碳排
    "costPools": {
        id:10328,
        unitTotal:"千元",
        unitSingle:"元/辆",
        titleTotal:"总成本能源类型占比排名",
        titleSingle:"单车成本能源类型占比排名"
    },//成本
    "summarySteams": {
        id:10324,
        unitTotal:"T",
        unitSingle:"T/辆",
        titleTotal:"总蒸汽能源类型占比排名",
        titleSingle:"单车蒸汽能源类型占比排名"
    },//蒸汽
    "aggregateWaters": {
        id:10317,
        unitTotal:"T",
        unitSingle:"T/辆",
        titleTotal:"总用水能源类型占比排名",
        titleSingle:"单车用水能源类型占比排名"
    },//用水
    "aggregateGass": {
        id:10310,
        unitTotal:"Nm³",
        unitSingle:"Nm³/辆",
        titleTotal:"总燃气能源类型占比排名",
        titleSingle:"单车燃气能源类型占比排名"
    },//燃气
    "aggregateElectricitys": {
        id:10303,
        unitTotal:"kwh",
        unitSingle:"kwh/辆",
        titleTotal:"总用电能源类型占比排名",
        titleSingle:"单车用电能源类型占比排名"
    },//用电
    "airCollectionss": {
        id:10294,
        unitTotal:"m³",
        unitSingle:"m³/辆",
        titleTotal:"总空气能源类型占比排名",
        titleSingle:"单车空气能源类型占比排名"
    },//空气
    "aggregateCoolings": {
        id:10286,
        unitTotal:"Kcal",
        unitSingle:"Kcal/辆",
        titleTotal:"总冷量能源类型占比排名",
        titleSingle:"单车冷量能源类型占比排名"
    }//冷量
};

dev.action = function(resp,param){

    var workSpaceId = iot.getEnergySpaceId(param.workSpaceName, param.name);
    var name = param.name
    var param= {
        id:energyDevId[param.name].id,
        time:param.time,
        selectType:param.selectType,
        workSpace:workSpaceId,
        workSpaceName: param.workSpaceName
    }
    var jsData = iot.bridgeOtherJsResult(JSON.stringify(param));
    var proportData = jsData.jsData;// 占比数据
    var rankData = iot.list();// 排名数据

    var datas = proportData.datas;
    // JS调用Java排序方法，根据value进行降序排列
    var Collections = Java.type("java.util.Collections");
    var Comparator = Java.type("java.util.Comparator")
    var Printer = Java.extend(Comparator, {
        compare : function (o1, o2) {
            var value1 = Number(o1.value);
            var value2 = Number(o2.value);
            if (value1 > value2) {
                return -1;
            }
            if (value1 > value2) {
                return 1;
            }
            return 0;
        }
    })
    Collections.sort(datas, new Printer());
    var totalSum = 0; // 总数
    for (var index in datas) {
        var value = Number(datas[index].value);
        totalSum += value;
    }
    for (var index in datas) {
        var value = datas[index].value;
        var proport = totalSum !== 0 ? value / totalSum : 0; // 占比
        datas[index].proport = numberFormate.format(proport * 100) + "%";
        datas[index].rank = (index + 1);
    }
    rankData.add(datas);

    // 组织数据
    /*var indicator = iot.list();
    var indicatorData = iot.list();


    var indicatorValue = {
        value: iot.list()
    };
    var max = 200.0;
    for (var index in datas) {
        var value = datas[index].value;
        var name = datas[index].name;
        var indicatorItem = {};
        indicatorItem.max = parseFloat(value) + max + '';
        indicatorItem.name = name;
        indicator.add(indicatorItem);
        indicatorValue.value.add(value);
    }
    indicatorData.add(indicatorValue);


    var jsData = {
        indicator:indicator,
        rankData:rankData,
        data:indicatorData
    }*/

    // 组织数据
    var rankDataSingle = iot.list();

    var spaces = param.workSpaceName.split('/');
    var factoryName = spaces[spaces.length-1]
    //start计算单车数据
    var db = iot.db();
    var totalYield = 0
    var pvYield;
    //获取产量
    var yieldId = getFactoryYieldId(factoryName)

    var yieldResult = getFactoryYield(yieldId, param.time)

    totalYield = yieldResult.totalSum;

    for (var index in datas) {
        var rankDataSingleItem = {
            value : "",
            name : "",
            rank : "",
            proport : ""
        }
        if (totalYield && totalYield !== 0) {
            rankDataSingleItem.value = decimalRetainRule(datas[index].value / totalYield);
        } else{
            rankDataSingleItem.value = "0.00";
        }

        if (index > 0 && rankDataSingle.get(index - 1).value === rankDataSingleItem.value) {
            rankDataSingleItem.rank = rankDataSingle.get(index - 1).rank;
        } else{
            rankDataSingleItem.rank = index + 1;
        }
        rankDataSingleItem.name = datas[index].name;

        rankDataSingle.add(rankDataSingleItem);
    }
    //end计算单车数据
    var totalSumSingle = 0; // 总数
    for (var index in rankDataSingle) {
        var value = Number(rankDataSingle[index].value);
        totalSumSingle += value;
    }
    for (var index in rankDataSingle) {
        var value = rankDataSingle[index].value;
        var proport = totalSumSingle !== 0 ? value / totalSumSingle : 0; // 占比
        rankDataSingle[index].proport = numberFormate.format(proport * 100) + "%";
    }

    for (var index in datas) {
        if (name === "collectChart" || name === "costPools") {
            datas[index].value = decimalRetainRule(datas[index].value / 1000)
        } else {
            datas[index].value = decimalRetainRule(datas[index].value)
        }
    }

    var rankDataTotalJson = {
       rankDataTotal:datas
       ,
       unit : energyDevId[name].unitTotal
       ,
       title : energyDevId[name].titleTotal
    }
    var rankDataSingleJson = {
       rankDataSingle : rankDataSingle
            ,
       unit : energyDevId[name].unitSingle
       ,
       title : energyDevId[name].titleSingle
   }

    var jsData = {
        rankDataTotal:rankDataTotalJson,
        rankDataSingle:rankDataSingleJson
    }
    resp.setJsData(jsData);
}